from ConfigParser import ConfigParser, NoOptionError
import os

DEFAULT_CONFIG = """
[config]
# Google Account email address (one associated w/ your Voice account)
gv_email=

# Raw password used or login
gv_password=

# Phone number to receive msgs from GV
gv_phone=

# If phone_auth==1, only messages from gv_phone is processed.
phone_auth=0

# Fetion Account
f_user=

# Fetion password
f_password=

"""

class Config(ConfigParser):
    """
    ``ConfigParser`` subclass that looks into your home folder for a file named
    ``.gvoice`` and parses configuration data from it.
    """
    def __init__(self):
        global DEFAULT_CONFIG
        self.fname = os.path.expanduser('~/.gvbridge')

        if not os.path.exists(self.fname):
            try:
                f = open(self.fname, 'w')
            except IOError:
                return
            f.write(DEFAULT_CONFIG)
            f.close()
            
        ConfigParser.__init__(self)
        try:
            self.read([self.fname])
        except IOError:
            return

    def get(self, option, section='config'):
        try:
            return ConfigParser.get(self, section, option).strip() or None
        except NoOptionError:
            return
        
    def set(self, option, value, section='config'):
        return ConfigParser.set(self, section, option, value)
        
    def save(self):
        f = open(self.fname, 'w')
        self.write(f)
        f.close()
        
    gv_email = property(lambda self: self.get('gv_email'))
    gv_password = property(lambda self: self.get('gv_password'))
    gv_phone = property(lambda self: self.get('gv_phone'))
    phone_auth = property(lambda self: self.get('phone_auth'))
    f_user = property(lambda self: self.get('f_user'))
    f_password = property(lambda self: self.get('f_password'))
    
config = Config()
